package com.twotiger.and.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import app.twotiger.p2p.R;
import app.twotiger.p2p.R.color;

import com.twotiger.and.activity.account.capitalflow.CapitalFlowDetailPage;
import com.twotiger.and.activity.base.BaseActivity;
import com.twotiger.and.bean.Trade;
import com.twotiger.and.util.ArithUtils;
import com.twotiger.and.util.DateUtil;
import com.twotiger.and.util.ListUtils;

/**
 * Created by victor on 2014/11/7 0007.
 */
public class DealDeatilAdapter extends BaseAdapter {
    private BaseActivity mactivity;
    private ArrayList<Trade> mList;

    private boolean isShowHead = true;

    public DealDeatilAdapter(Activity _Activity, ArrayList<Trade> _List) {
        this.mactivity = (BaseActivity) _Activity;
        this.mList = _List;
    }

    public void resetData(ArrayList<Trade> _List) {
        mList.clear();
        if (!ListUtils.isEmpty(_List)) {
            mList.addAll(_List);
        }
        notifyDataSetChanged();
    }

    public void addData(ArrayList<Trade> _List) {
        if (!ListUtils.isEmpty(_List)) {
            mList.addAll(_List);
        }
        notifyDataSetChanged();
    }

    public void setHeader(boolean boo) {
        isShowHead = boo;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (!ListUtils.isEmpty(mList)) {
            return mList.size();
        }
        return 1;
    }

    @Override
    public Trade getItem(int position) {
        if (!ListUtils.isEmpty(mList) && position < mList.size()) {
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        MViewHolder mMViewHolder = null;

        if (convertView == null) {
            mMViewHolder = new MViewHolder();
            convertView = LayoutInflater.from(mactivity).inflate(R.layout.deal_detail_item, null);
            mMViewHolder.ll_header = (LinearLayout) convertView.findViewById(R.id.ll_header);
            mMViewHolder.tv_available = (TextView) convertView.findViewById(R.id.tv_available);
            mMViewHolder.op_time = (TextView) convertView.findViewById(R.id.op_time);
            mMViewHolder.capital_from = (TextView) convertView.findViewById(R.id.capital_from);
            mMViewHolder.banlance_value = (TextView) convertView.findViewById(R.id.banlance_value);
            mMViewHolder.capital_value = (TextView) convertView.findViewById(R.id.capital_value);
            convertView.setTag(mMViewHolder);

        } else {
            mMViewHolder = (MViewHolder) convertView.getTag();
        }

        final Trade trade = getItem(position);
        if (trade != null) {
            mMViewHolder.capital_from.setText(trade.getTypePName() + "-" + trade.getTypeName());
            mMViewHolder.banlance_value.setText(String.valueOf("余额："
                                                               + ArithUtils.coverMoneyComma(trade
                                                                   .getAvaliable() + "")));
            switch (trade.getIsIncome()) {
            case 1:
                mMViewHolder.capital_value.setText("+"
                                                   + ArithUtils.coverMoneyComma(trade.getAmount()
                                                                                + ""));
                mMViewHolder.capital_value.setTextColor(mactivity.getResources().getColor(
                    color.capitalflow_income));
                break;
            case -1:
                mMViewHolder.capital_value.setText("-"
                                                   + ArithUtils.coverMoneyComma(trade.getAmount()
                                                                                + ""));
                mMViewHolder.capital_value.setTextColor(mactivity.getResources().getColor(
                    color.capitalflow_not_income));
                break;
            case 0:
                mMViewHolder.capital_value.setText(""
                                                   + ArithUtils.coverMoneyComma(trade.getAmount()
                                                                                + ""));
                break;
            default:
                break;
            }
            mMViewHolder.op_time.setText(DateUtil.getDateStringForLong(
                Long.valueOf(trade.getCtime()), DateUtil.FMT));//trade.get

            convertView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mactivity, CapitalFlowDetailPage.class).putExtra(
                        "FLOWDETAIL", trade);
                    mactivity.startNewActivity(intent, R.anim.push_left_in, R.anim.push_left_out,
                        false);
                }
            });
            switch (trade.getType()) {
            case 0:

                break;

            default:
                break;
            }
            if (position != 0) {
                mMViewHolder.ll_header.setVisibility(View.GONE);
            } else {
                if (isShowHead) {
                    mMViewHolder.ll_header.setVisibility(View.VISIBLE);
                    mMViewHolder.tv_available.setText(ArithUtils
                        .coverMoneyComma(mactivity.application.getUserAsset().avaliable + ""));
                } else {
                    mMViewHolder.ll_header.setVisibility(View.GONE);
                }
            }
        }

        return convertView;

    }

    class MViewHolder {
        TextView op_time;
        TextView capital_from;
        TextView banlance_value;
        TextView capital_value;
        LinearLayout ll_header;
        TextView tv_available;
    }
}
